package ui;

import java.awt.BorderLayout;
import java.awt.GridLayout;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JMenuBar;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import javax.swing.JTextField;

import config.ConfigType;

import define.Config;

public class Window
{
	private JFrame m_mainWindow;
	private JPanel m_mainPanel;

	private JPanel m_downPanel;
	
	private JTextArea m_text;
	private JTextField m_scriptName;
	private JScrollPane m_scroll;
	private JButton m_open;
	private JButton m_compile;
	private JButton m_start;
	private JButton m_go;
	private JButton m_add;
	
	private JMenuBar m_menuBar;
	private MenuGroup[] m_menuGroups;
	
	public static Window m_instance;
	
	public Window()
	{
		m_mainWindow = new JFrame("Excel2-" + Config.VERSION);
		m_mainPanel = new JPanel();
		m_mainWindow.setContentPane(m_mainPanel);
		m_mainWindow.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		m_mainWindow.setSize(Config.WINDOW_W, Config.WINDOW_H);
		m_menuBar = new JMenuBar();
		m_mainWindow.setJMenuBar(m_menuBar);
		m_menuGroups = new MenuGroup[3];
		m_menuGroups[Config.MENU_FONT_GROUP] = new MenuGroup(Config.MENU_FONT, Config.MENU_FONT_GROUP);
		m_menuGroups[Config.MENU_OUTPUT_GROUP] = new MenuGroup(Config.MENU_OUTPUT, Config.MENU_OUTPUT_GROUP);
		m_menuGroups[Config.MENU_ATLAS_GROUP] = new MenuGroup(Config.MENU_ATLAS, Config.MENU_ATLAS_GROUP);
		for(MenuGroup mg: m_menuGroups)
		{
			mg.addToMenuBar(m_menuBar);
		}
		
		m_downPanel = new JPanel();
		m_mainPanel.setLayout(new BorderLayout());
		m_text = new JTextArea();
		m_scroll = new JScrollPane(m_text, JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED, JScrollPane.HORIZONTAL_SCROLLBAR_NEVER);
		m_mainPanel.add(m_scroll, BorderLayout.CENTER);
		m_text.setLineWrap(true);
		m_text.setEditable(false);
		m_mainPanel.add(m_downPanel, BorderLayout.SOUTH);
		
		m_scriptName = new JTextField();
		m_open = new JButton("open");
		m_compile = new JButton("compile");
		m_start = new JButton("start");
		m_go = new JButton("go");
		m_add = new JButton("add");
		m_downPanel.setLayout(new GridLayout(2, 3));
		m_downPanel.add(m_scriptName);
		m_downPanel.add(m_open);
		m_downPanel.add(m_add);
		m_downPanel.add(m_compile);
		m_downPanel.add(m_start);
		m_downPanel.add(m_go);
		m_open.addMouseListener(new OpenHandle(false));
		m_add.addMouseListener(new OpenHandle(true));
		m_compile.addMouseListener(new CompileHandle());
		m_start.addMouseListener(new StartHandle());
		m_go.addMouseListener(new GoHandle());
		setButtonGo(false);
		m_mainWindow.setVisible(true);
		m_instance = this;
	}
	
	public void changeMenuText(ConfigType ct)
	{
		m_menuGroups[ct.getGroup()].changeText(ct);
	}
	
	public JFrame getMainWindow()
	{
		return m_mainWindow;
	}
	
	public String getScriptName()
	{
		return m_scriptName.getText();
	}
	
	public void addText(String text)
	{
		m_text.setText(m_text.getText() + text + "\n");
	}
	
	public void clearText()
	{
		m_text.setText("");
	}
	
	public void setButtonGo(boolean isGo)
	{
		m_open.setEnabled(!isGo);
		m_go.setEnabled(isGo);
		m_add.setEnabled(!isGo);
		m_compile.setEnabled(!isGo);
		m_start.setEnabled(!isGo);
	}
}
